// 云函数入口文件
const cloud = require('wx-server-sdk');
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV,
});

exports.main = async (event, context) => {
  const { ENV, OPENID, APPID } = cloud.getWXContext();
  // 更新默认配置，将默认访问环境设为当前云函数所在环境
  cloud.updateConfig({
    env: ENV,
  });
  const db = cloud.database({
    env: ENV,
  });
  const _ = db.command;
  const { type, data } = event;
  const {_id} = data || {};
  // 业务逻辑
  switch (type) {
    case 'addone':
      return await db.collection('polygons').add({
        data: {
          ...data,
        },
      });
    case 'getall':
      return await db.collection('polygons').get();
    case 'getone':
      return await db.collection('polygons').doc(_id).get();
    default:
      return await '参数有误';
  }
}